﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Media;
using System.Xml.Linq;
using TheCollectiveNetwork.Web.Services;
using TheCollectiveNetwork.Classes.Security;
using TheCollectiveNetwork.Views;
using TheCollectiveNetwork.CustomControls;
using TheCollectiveNetwork.Classes.GlobalConstantsVariables;
using TheCollectiveNetwork.Classes.GlobalFunctions;
using TCNDataAccessLayer;

namespace TheCollectiveNetwork.Views.Administration
{
    public partial class RegistrationRequests : Page
    {
        // Define Variables
        UserManagementContext _umc;


        public RegistrationRequests()
        {
           
            // Ensure that only admin users can access this page
            if (TCNauthentication.isAdminUser)
            {
                InitializeComponent();
                _umc = new UserManagementContext();

                
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            LoadRegistrationRequests();
        }


        /* ------------ Button Clicks --------------- */

        private void RegistrationApproveButton_Click(object sender, EventArgs e)
           {
               // Define variables
               InvokeOperation iop;
               XElement processlist;
               SolidColorBrush scb_green;

               // Initialise variables
               scb_green = new SolidColorBrush(Colors.Green);
               RegistrationMessage_CustomList.Clear();
               
               // Display the busy indicator
               ProcessingIndicator.DisplayApplicationBusy();

               // Create the XML Fragment
               processlist = new XElement("processlist");
               
               // Loop through and add items ticked for processing to an xml fragment
               foreach(DTOAccountsRegisterationRequest DTOarr in UserListDataGrid.ItemsSource )
               {
                   if (DTOarr.process)
                   {
                       XElement user = new XElement("regrequest");
                       user.SetAttributeValue("GuidID", DTOarr.User_GUID);
                       processlist.Add(user);                       
                   };
               };

               // Send the XML processing fragment to the domain service as a string
               iop = _umc.ApproveRegistrationRequest(processlist.ToString());

               // Once the operation has completed refresh the list
               iop.Completed += (s, ea) =>
               {
                   LoadRegistrationRequests();
                   
                   // Hide the busy indicator
                   ProcessingIndicator.HideApplicationBusy();
                   RegistrationMessage_CustomList.Add(iop.Value.ToString(), scb_green);                   
               }; 
               

            


           }


        private void RegistrationDeleteButton_Click(object sender, EventArgs e)
        {
            // Define variables
            InvokeOperation iop;
            XElement processlist;           
            SolidColorBrush scb_green;

            // Initialise variables            
            scb_green = new SolidColorBrush(Colors.Green);
            RegistrationMessage_CustomList.Clear();

            // Display the busy indicator
            ProcessingIndicator.DisplayApplicationBusy();

            // Create the XML Fragment
            processlist = new XElement("processlist");

            // Loop through and add items ticked for processing to an xml fragment
            foreach (DTOAccountsRegisterationRequest DTOarr in UserListDataGrid.ItemsSource)
            {
                if (DTOarr.process)
                {
                    XElement user = new XElement("regrequest");
                    user.SetAttributeValue("GuidID", DTOarr.User_GUID);
                    processlist.Add(user);
                };
            };

            // Send the XML processing fragment to the domain service as a string
            iop = _umc.DeleteRegistrationRequest(processlist.ToString());

            // Once the operation has completed refresh the list
            iop.Completed += (s, ea) =>
            {
                LoadRegistrationRequests();
                
                // Hide the busy indicator
                ProcessingIndicator.HideApplicationBusy();
                RegistrationMessage_CustomList.Add(iop.Value.ToString(), scb_green);
            }; 
               


        }
               

        private void SelectAllCheckBox_Click(object sender, EventArgs e)
        {
            CheckBox cbox;
            cbox = (CheckBox)sender;

            // Set the state of the process checkboxes in the datagrid to match the select all checkboxes
            foreach (DTOAccountsRegisterationRequest DTOarr in UserListDataGrid.ItemsSource)
            {
                DTOarr.process = cbox.IsChecked ?? false;                
            }
        }

        private void RegistrationSelectCheckBox_Click(object sender, EventArgs e)
        {
            SelectAllCheckBox_CheckPage();
        }

        private void UserListDataPager_PageIndexChanged(object sender, EventArgs e)
        {
            SelectAllCheckBox_CheckPage();           
        }

        private void UserListDataPager_PageIndexChanging(object sender, EventArgs e)
        {
            RegistrationSelectCheckBox_UntickAllPage();
        }
       
        /* -------------- Private Functions ----------------- */
         // Loads the list of registration requests
        private void LoadRegistrationRequests()
        {
            // Display the busy indicator
            ProcessingIndicator.DisplayApplicationBusy();

            // Ensure select all checkbox is selected
            SelectAllCheckBox.IsChecked = false;

            // Load the data
            LoadOperation<DTOAccountsRegisterationRequest> loadOp = _umc.Load<DTOAccountsRegisterationRequest>(_umc.GetAccounts_Registration_RequestsQuery());

            loadOp.Completed += (s, ea) =>
            {
                UserListDataPager.Source = new PagedCollectionView(loadOp.Entities);
                UserListDataGrid.ItemsSource = UserListDataPager.Source;

                // Hide the busy indicator
                ProcessingIndicator.HideApplicationBusy();
            };
        }

        // Ensures the select all checkbox is unticked if any of the items on the page are unticked
        private void SelectAllCheckBox_CheckPage()
        {
            // Variable definitions
            bool untickedfound;

            // Loop through each item on the page and check if any process checkbox is unticked
            untickedfound = false;

            if (UserListDataGrid.ItemsSource != null)
            {
                foreach (DTOAccountsRegisterationRequest DTOarr in UserListDataGrid.ItemsSource)
                {
                    if (DTOarr.process == false)
                    {
                        untickedfound = true;
                    };
                };
            }
            
            // Untick the select all checkbox
            if (untickedfound)
            {
                SelectAllCheckBox.IsChecked = false;
            }
            
        }


        // Unticks all the process tick boxes on the page
        private void RegistrationSelectCheckBox_UntickAllPage()
        {           

            if (UserListDataGrid.ItemsSource != null)
            {
                foreach (DTOAccountsRegisterationRequest DTOarr in UserListDataGrid.ItemsSource)
                {
                    DTOarr.process = false;                    
                };
            }
                      

        }

    }
}
